import { UploadOutlined } from '@ant-design/icons';
import { Button, Form, Upload } from 'antd';



const formItemLayout = {
    labelCol: {
        span: 6,
    },
    wrapperCol: {
        span: 14,
    },
};



const normFile = (e) => {
    console.log('Upload event:', e);
    const formData = new FormData()
    formData.append('file', e)

    if (Array.isArray(e)) {
        return e;
    }

    return e?.fileList;
};

const beforeUpload = ({ fileList }) => {
    return false;
}



const App = () => {
    const onFinish = (values) => {
        console.log('Received values of form: ', values);

        fetch('http://43.142.95.88:4545/upload', {
            method: "POST",
            headers: new Headers({
                "Authorization": 'Basic YWRtaW5AcXVpeG90ZV9sY3M6aHViZWlAbGNzXzEyMDg=',
            }),
            body: values
        }).then(
            (response) => {
                response.json()
                console.log("response111111111111111111", response)
            }
        ).then(
            (result) => {
                console.log("2222222222222")
            }
        )



    };

    return (
        <Form
            name="validate_other"
            {...formItemLayout}
            // 提交表单且数据验证成功后回调事件
            onFinish={onFinish}
        // 初始化值
        // initialValues={{
        //     'input-number': 3,
        //     'checkbox-group': ['A', 'B'],
        //     rate: 3.5,
        // }}
        >

            <Form.Item
                name="upload"
                label="Upload"
                valuePropName="fileList"
                // 设置如何将event的值转换成字段值
                getValueFromEvent={normFile}
                extra="long"
            >
                <Upload
                    // 发给后台的文件参数名
                    // name="logo"
                    // 上传的地址
                    // action="/upload.do"
                    beforeUpload={beforeUpload}
                >
                    <Button icon={<UploadOutlined />}>点击上传</Button>
                </Upload>
            </Form.Item>


            <Form.Item
                wrapperCol={{
                    span: 12,
                    offset: 6,
                }}
            >
                <Button type="primary" htmlType="submit">
                    提交
                </Button>
            </Form.Item>
        </Form>
    );
};

export default App;